package hikari.objects3d;

import com.gooddies.wiring.Wiring;
import hikari.gui.assets.AssetsTree;
import hikari.gui.panels.MaterialPropertiesPanel;
import hikari.hcomponents.HComponent;
import hikari.persistence.file.FileSection;
import hikari.persistence.file.PersistenceSection;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.swig.jirr.MATERIAL_FLAG;
import org.swig.jirr.SMaterial;

/**
 * @author Dmitry
 */
public abstract class GeometricObject extends Basic3dObject {

    public GeometricObject(String objectName) {
        super(objectName, false);
    }

    public GeometricObject(String objectName, boolean generateIndex) {
        super(objectName, generateIndex);
    }

    private MaterialPropertiesPanel getMaterialPropertiesPanel() {
        MaterialPropertiesPanel panel = Wiring.getComponent(MaterialPropertiesPanel.class);
        panel.initialUpdateFromModel(this);
        return panel;
    }

    @Override
    public List<HComponent> getEditPanels() {
        List<HComponent> panels = super.getEditPanels();
        panels.add(getMaterialPropertiesPanel());
        return panels;
    }

    private void saveMaterial(SMaterial material, PersistenceSection section) throws IOException {
        section.writeBoolean("vis", isVisible());
        if (getMeshSceneNode().isAnyFlagChanged()) {
            PersistenceSection flagsSection = section.createNewSection("flags");
            for (MATERIAL_FLAG flag : MATERIAL_FLAG.values()) {
                if (getMeshSceneNode().isFlagChanged(flag)) {
                    long flagId = flag.getCPtr();
                    long flagValue = material.getFlag(flag) ? 1 : 0;
                    flagsSection.writeLongs("flag", new long[]{flagId, flagValue});
                }
            }

            flagsSection.close();
        }

        PersistenceSection texturesSection = section.createNewSection("textures");
        int texturesCount = material.getTexturesCount();
        for (int i = 0; i < texturesCount; i++) {
            String fullPath = material.getTexture(i).getName().getInternalName();
            String path =  Wiring.getComponent(AssetsTree.class).getRelativeAssetsPath(fullPath);
            texturesSection.writeString("path", path);
        }
        texturesSection.writeLong("matType", material.getMaterialType().cptr());
        texturesSection.close();

        if (getMeshSceneNode().isAnyColorChanged()) {
            PersistenceSection colorsSection = section.createNewSection("colors");
            if (getMeshSceneNode().isAmbientChanged()) {
                colorsSection.writeSColor("a", material.getAmbientColor());
            }
            if (getMeshSceneNode().isDiffuseChanged()) {
                colorsSection.writeSColor("d", material.getDiffuseColor());
            }
            if (getMeshSceneNode().isEmmisiveChanged()) {
                colorsSection.writeSColor("e", material.getEmissiveColor());
            }
            if (getMeshSceneNode().isSpecularChanged()) {
                colorsSection.writeSColor("spec", material.getSpecularColor());
            }
            if (getMeshSceneNode().isShinessChanged()) {
                colorsSection.writeFloat("shin", material.getShininess());
            }

            colorsSection.close();
        }
    }

    @Override
    public void writeData(FileSection section) throws IOException {
        super.writeData(section);
        PersistenceSection materialSection = section.createNewSection("material");
        saveMaterial(getMeshSceneNode().getMaterial(0), materialSection);
        materialSection.close();
    }
}
